/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. A copy of the
 * WTFPL version 2 is provided in the source archive, at
 * example/WTFPL-2 .*/
#include <fake437.h>

static F437Display* display;

static F437DisplayLayer* layer1;
static int layer1_show = 0;
static int layer1_left = 3;
static int layer1_top = 10;

static F437DisplayLayer* layer2;
static int layer2_show = 0;
static int layer2_left = 8;
static int layer2_top = 12;

static void toggle_layer1(void) {
  if (layer1_show) {
    f437_display_remove_layer(display, 1);
  } else {
    f437_display_insert_layer(display, layer1, 1);
  }
  layer1_show = !layer1_show;
}

static void move_layer1(int dx, int dy) {
  layer1->left += dx;
  layer1->top += dy;
}

static void toggle_layer2(void) {
  if (layer2_show) {
    f437_display_pop_layer(display);
  } else {
    f437_display_push_layer(display, layer2);
  }
  layer2_show = !layer2_show;
}

static void move_layer2(int dx, int dy) {
  layer2->left += dx;
  layer2->top += dy;
}

int main(int argc, char* argv[]) {
  SDL_Event ev;
  SDL_Surface* screen;
  F437DisplayLayer* layer;
  int done = 0;
  int i;
  (void)argc;
  (void)argv;
  SDL_Init(SDL_INIT_VIDEO);

  screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE);
  SDL_WM_SetCaption("Display test - ESC to quit",
                    "Display test - ESC to quit");
  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
                      SDL_DEFAULT_REPEAT_INTERVAL);

  display = f437_display_new(screen, f437_get_font(F437_FONT_8x8_THIN));
  layer = f437_display_layer_new(0, 0, 1024 / 8, 768 / 8);
  for (i = 0 ; i < (1024 * 768) / 64 ; i++) {
    layer->cells[i].ch = i % 256;
    layer->cells[i].fg = F437_COLOR_BGREEN;
  }
  f437_display_push_layer(display, layer);

  layer1 = f437_display_layer_new(layer1_left, layer1_top, 30, 30);
  for (i = 0 ; i < 30 * 30 ; i++) {
    layer1->cells[i].ch = 2;
    layer1->cells[i].fg = F437_COLOR_BWHITE;
    layer1->cells[i].bg = F437_COLOR_BLUE;
  }

  layer2 = f437_display_layer_new(layer2_left, layer2_top, 20, 40);
  for (i = 0 ; i < 20 * 40 ; i++) {
    layer2->cells[i].ch = 227;
    layer2->cells[i].fg = F437_COLOR_MAGENTA;
    layer2->cells[i].bg = F437_COLOR_TRANSPARENT;
  }

  while (!done) {
    f437_display_draw(display);
    SDL_Flip(screen);
    while (SDL_PollEvent(&ev)) {
      if (ev.type == SDL_KEYDOWN) {
        switch (ev.key.keysym.sym) {
        case SDLK_ESCAPE: done = 1; break;
        case SDLK_1: toggle_layer1(); break;
        case SDLK_a: move_layer1(-1, 0); break;
        case SDLK_s: move_layer1(0, 1); break;
        case SDLK_d: move_layer1(0, -1); break;
        case SDLK_f: move_layer1(1, 0); break;
        case SDLK_2: toggle_layer2(); break;
        case SDLK_h: move_layer2(-1, 0); break;
        case SDLK_j: move_layer2(0, 1); break;
        case SDLK_k: move_layer2(0, -1); break;
        case SDLK_l: move_layer2(1, 0); break;
        default: break;
        }
      }
    }
  }

  SDL_Quit();
  return 0;
}
